<?php

class pages extends Module {
	
	public function __construct () {
		parent::__construct (__CLASS__);
	}
	
	public function __default () {
		$oTypePage = TypesController::getInstance()->getTypeByName ('page');
		$objectsController = ObjectsController::getInstance ();
		
		$sel = new Selection;
		$sel->addType ($oTypePage);
		$sel->setOrder ('title');
		
		$aObjects = $objectsController->getObjectsList($sel->getList ());
		
		$response = new ModuleResponse;
		$response->setTemplate ('pages/index');
		
		if (getRequest ('added','bool')) {
			$response->addObject (new SystemMessage (lang('page_added','pages')));
		}
		if (getRequest ('saved','bool')) {
			$response->addObject (new SystemMessage (lang('page_saved','pages')));
		}
		if (getRequest ('deleted','bool')) {
			$response->addObject (new SystemMessage (lang('page_deleted','pages')));
		}
		
		foreach ($aObjects as $object) {
			$response->addObject ($object);
		}
		
		return $response;
	}
	
	public function add () {
		$oTypePage = TypesController::getInstance()->getTypeByName ('page');
		
		$form = new Form ($oTypePage, 'new');
		$form->buildFromType ();
		
		$aErrors = array ();
		
		if (getRequest ('param0') == 'save') {
			$aValues = $this->getRequestData ('new', $oTypePage);
			$form->setValues ($aValues);
			
			$object = new Object ($oTypePage);
			$object->setValues ($aValues);
			
			$aErrors = $this->getValidateErrors ($object);
			
			if (empty ($aErrors['alias'])) {
				$testSel = new Selection;
				$testSel->addType ($oTypePage);
				$testSel->addFilter ('alias', $object->getValue ('alias'));
				
				if ($testSel->getTotalCount () > 0) {
					$aErrors['alias'] = lang ('error_save_page_alias_exists','pages');
				}
			}
			
			if (!count ($aErrors)) {
				$object->createNew ();
				redirect (HTTP_HOST . '/admin/pages/?added');				
			}
		}
		
		$form->setErrors ($aErrors);
		
		$response = new ModuleResponse;
		$response->setTemplate ('pages/form');
		$response->addObject ($form);
		
		return $response;
	}
	
	public function edit () {
		$oTypePage = TypesController::getInstance()->getTypeByName ('page');
		
		$pageId = getRequest ('param0', 'int');
		
		if (!$object = ObjectsController::getInstance()->getObject ($oTypePage, $pageId)) {
			throw new NotFound;
		}
		
		$form = new Form ($oTypePage, $pageId);
		$form->buildFromType ();
		
		$aErrors = array ();
		$aValues = $object->getValues ();
		
		if (getRequest ('param1') == 'save') {
			$aValues = $this->getRequestData ($pageId, $oTypePage);
			$form->setValues ($aValues);
			
			$object->setValues ($aValues);
			
			$aErrors = $this->getValidateErrors ($object);
			
			if (empty ($aErrors['alias'])) {
				$testSel = new Selection;
				$testSel->addType ($oTypePage);
				$testSel->addFilter ('alias', $object->getValue ('alias'));
				$testSel->addFilter ('id', $pageId, '!=');
				
				if ($testSel->getTotalCount () > 0) {
					$aErrors['alias'] = lang ('error_save_page_alias_exists','pages');
				}
			}
			
			if (!count ($aErrors)) {
				$object->save ();
				redirect (HTTP_HOST . '/admin/pages/?saved');
				
			}
		}
		
		$form->setErrors ($aErrors);
		$form->setValues ($aValues);
		
		$response = new ModuleResponse;
		$response->setTemplate ('pages/form');
		$response->addObject ($form);
		
		return $response;
	}
	
	public function delete () {
		$this->expectSecureGET();
		
		if (!$object = ObjectsController::getInstance()->getObject (TypesController::getInstance()->getTypeByName ('page'), getRequest ('param0', 'int'))) {
			throw new NotFound;
		}
		
		$object->delete ();
		redirect (HTTP_HOST . '/admin/pages/?deleted');
	}
	
}